package design.simdi.code.final_paper.site.application;

import org.apache.wicket.Page;
import org.apache.wicket.Request;
import org.apache.wicket.RequestCycle;
import org.apache.wicket.Response;
import org.apache.wicket.Session;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.authentication.AuthenticatedWebApplication;
import org.apache.wicket.authentication.AuthenticatedWebSession;
import org.apache.wicket.injection.web.InjectorHolder;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.protocol.http.PageExpiredException;
import org.apache.wicket.protocol.http.WebRequest;
import org.apache.wicket.protocol.http.WebResponse;
import org.apache.wicket.request.target.coding.QueryStringUrlCodingStrategy;
import org.apache.wicket.spring.injection.annot.SpringBean;
import org.apache.wicket.spring.injection.annot.SpringComponentInjector;

import design.simdi.code.final_paper.core.daos.UserDao;
import design.simdi.code.final_paper.core.entities.User;
import design.simdi.code.final_paper.site.pages.BasePage;
import design.simdi.code.final_paper.site.pages.HomePage;
import design.simdi.code.final_paper.site.pages.LoginPage;

/**
 * Application object for your web application. If you want to run this
 * application without deploying, run the Start class.
 * 
 * @see design.simdi.code.final_paper.Start#main(String[])
 */
public class WicketApplication extends AuthenticatedWebApplication

{
	private boolean showErrors;
	@SpringBean
	private UserDao userDao;

	@Override
	protected void init() {
		super.init();
		// setApplicationContext(WebApplicationContextUtils
		// .getRequiredWebApplicationContext(getServletContext()));
		setListeners();
		mount(new QueryStringUrlCodingStrategy("home", HomePage.class));
		mount(new QueryStringUrlCodingStrategy("login", LoginPage.class));
		InjectorHolder.getInjector().inject(this);

	}

	public final RequestCycle newRequestCycle(final Request request, final Response response) {
		return new MyRequestCycle(this, (WebRequest) request, (WebResponse) response);
	}

	public AjaxRequestTarget newAjaxRequestTarget(final Page page) {

		User currentUser = ((MyAuthenticatedWebSession) Session.get()).getCurrentUser();

		if (currentUser != null) {
			currentUser = userDao.get(currentUser.getId());
			switch (currentUser.getStatus()) {

			case BLOCKED:
			case REQUEST_ACTIVATE:
			case MARK_FOR_DELETE:
				currentUser = null;
				Session.get().invalidateNow();
				throw new PageExpiredException("contul utilizatorului curent a fost sters, sau blocat");
			}
		}

		AjaxRequestTarget target = new AjaxRequestTarget(page);

		if (page instanceof BasePage && isShowErrors()) {
			BasePage basePage = (BasePage) page;
			target.addComponent(basePage.get(BasePage.ERRORS_PANEL));
		}

		return target;
	}

	private void setListeners() {
		addComponentInstantiationListener(new SpringComponentInjector(this));

	}

	@Override
	protected Class<? extends AuthenticatedWebSession> getWebSessionClass() {
		return MyAuthenticatedWebSession.class;
	}

	/**
	 * Constructor
	 */
	public WicketApplication() {

	}

	/**
	 * @see org.apache.wicket.Application#getHomePage()
	 */
	public Class<HomePage> getHomePage() {
		return HomePage.class;
	}

	@Override
	protected Class<? extends WebPage> getSignInPageClass() {
		return LoginPage.class;
	}

	public void setShowErrors(boolean showErrors) {
		this.showErrors = showErrors;
	}

	public boolean isShowErrors() {
		return showErrors;
	}

	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}

	public UserDao getUserDao() {
		return userDao;
	}

}
